﻿using System;
using System.Threading;
using System.Windows;
using JiaBao.Core.Infrastructure;
using JiaBao.Core.Plugins;
using JiaBao.WPF.Framework.SN;

namespace JiaBao.WPF
{

    static class AppStart
    {

        /// <summary>
        /// 0 退出
        /// 1 ok
        /// 2 重启
        /// </summary>
        public static int type = 0;

        [STAThread]
        static void Main(string[] args)
        {
            try
            {

                new Mutex(true, System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString(), out bool ret);
                if (!ret)
                {
                    MessageBox.Show("已有一个程序实例运行");
                    return;
                }


                //必须放首位
                PluginManager.Initialize();
                EngineContext.Initialize(false);

                //软件注册
                var register = EngineContext.Current.Resolve<Register>();
                if (!register.Verification())
                    if (new W_Register().ShowDialog() != true)
                        return;

                //初始化
                App app = new App();
                app.InitializeComponent();


                //必须放这里  不然会直接退出 不知道原因
                var loading = EngineContext.Current.Resolve<Loading>();
                var login = EngineContext.Current.Resolve<Login>();
                var w = EngineContext.Current.Resolve<MainWindow>();
                //var w = EngineContext.Current.Resolve<TestMain>(); ;

                //数据库检测
                app.Dispatcher.Invoke(() => loading.ShowDialog());

                if (1 != type)
                    return;

                ////登录
                var l = login.ShowDialog();
                if (l != true)
                    return;

                app.Run(w);
            }
            catch (Exception ex )
            {
                MessageBox.Show(ex.Message);
            }


        }




    }

}
